//
// Created by corey on 2021/4/13.
//

#ifndef CORNETNET_LITE_TRT_COMMON_H
#define CORNETNET_LITE_TRT_COMMON_H

#include <vector>
#include <chrono>
#include <sstream>
#include <fstream>
#include <memory>

namespace hackathon
{
    struct CommandParam
    {
        std::string model_path;
        int batch_size;
        bool use_int8;
    };

    struct InferDeleter
    {
        template<typename T>
        void operator()(T *obj) const
        {
            if (obj)
            {
                obj->destroy();
            }
        }
    };

    template<typename T>
    using UniquePtr = std::unique_ptr<T, InferDeleter>;

    // Logger for TensorRT info/warning/errors
    class Logger : public nvinfer1::ILogger
    {
    public:

        Logger(): Logger(Severity::kINFO) {}

        Logger(Severity severity): reportableSeverity(severity) {}

        void log(Severity severity, const char* msg) override
        {
            // suppress messages with severity enum value greater than the reportable
            if (severity > reportableSeverity) return;

            switch (severity)
            {
                case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: "; break;
                case Severity::kERROR: std::cerr << "ERROR: "; break;
                case Severity::kWARNING: std::cerr << "WARNING: "; break;
                case Severity::kINFO: std::cerr << "INFO: "; break;
                default: std::cerr << "UNKNOWN: "; break;
            }
            std::cerr << msg << std::endl;
        }

        Severity reportableSeverity{Severity::kWARNING};
    };

    /*
    * @brief: 判断文件是否存在
    * @param: [in] file : 文件路径
    * @ret:   true: 存在　false: 不存在
    * */
    inline bool check_file_exist(const std::string &file) {
        std::ifstream f(file.c_str());
        return f.good();
    }
}

#endif //CORNETNET_LITE_TRT_COMMON_H
